﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Medicine.Dao;
using Medicine.Entity;
namespace Medicine.Service
{
   
    class RecordService
    {
        private RecordDao rd = new RecordDao();

        public void buyMedicine(Medicine.Entity.Record record) {

            String sql = "insert into record (use_id,medicine_id,t,adress,remark) "
                + "values(" + record.User_id + "," + record.Medicine_id
                + ",'" + record.Time + "',"
                + "'" + record.Adress + "',"
                +"'"+record.Remark+"'"
                +")";
            rd.insertRecord(sql);
        }
      /// <summary>
      /// 查询用户的购买记录信息
      /// </summary>
      /// <returns></returns>
        public List<RecordDetail> queryUserRecord() {
            List<RecordDetail> rdlist = new List<RecordDetail>();
            String sql = "SELECT medicine.name,medicine.price,medicine.info,record.adress,record.remark,record.t"
                +" FROM record INNER JOIN medicine ON  medicine.id= record.medicine_id";
            List<Dictionary<String, Object>>  ret_list = rd.queryUserRecord(sql);//得到封装好的数据list
            foreach (Dictionary<String, Object> dic in ret_list)
            {
                RecordDetail rd = new RecordDetail();
         
                rd.Med_name = dic["name"].ToString();
                rd.Med_price = dic["price"].ToString();
                //MessageBox.Show(dic["price"].ToString());
                rd.Med_info =dic["info"].ToString();
                rd.Address = dic["address"].ToString();
                rd.Remark = dic["remark"].ToString();
                rd.Time = dic["time"].ToString();
                rdlist.Add(rd);//加入list做返会
            }
            return rdlist;
        }

        public List<RecordDetail> queryRecordByMedName(String med_name)
        {
            List<RecordDetail> rdlist = new List<RecordDetail>();
            String sql = "SELECT medicine.name,medicine.price,medicine.info,record.adress,record.remark,record.t"
                + " FROM record INNER JOIN medicine ON  medicine.id= record.medicine_id and medicine.name='"+med_name+"'";
            List<Dictionary<String, Object>> ret_list = rd.queryUserRecord(sql);//得到封装好的数据list
            foreach (Dictionary<String, Object> dic in ret_list)
            {
                RecordDetail rd = new RecordDetail();

                rd.Med_name = dic["name"].ToString();
                rd.Med_price = dic["price"].ToString();
                //MessageBox.Show(dic["price"].ToString());
                rd.Med_info = dic["info"].ToString();
                rd.Address = dic["address"].ToString();
                rd.Remark = dic["remark"].ToString();
                rd.Time = dic["time"].ToString();
                rdlist.Add(rd);//加入list做返会
            }
            return rdlist;
        }
        /// <summary>
        /// 按照商品的名称查询
        /// </summary>
        /// <param name="name">商品名称</param>
        /// <returns>查询后得到的信息</returns>
        public List<RecordDetail> queryAll(String name)
        {
            String sql=null;
            List<RecordDetail> rdlist = new List<RecordDetail>();
            if (name != null)
            {
                sql = "SELECT medicine.name,medicine.price,medicine.info,record.id,record.use_id,record.adress,record.remark,record.t"
               + " FROM record INNER JOIN medicine ON  medicine.id= record.medicine_id and medicine.name='" + name + "'";
            }
            else
            {
                sql = "SELECT medicine.name,medicine.price,medicine.info,record.id,record.use_id,record.adress,record.remark,record.t"
               + " FROM record INNER JOIN medicine ON  medicine.id= record.medicine_id";
            }
            List<Dictionary<String, Object>> ret_list = rd.queryUserRecord1(sql);//得到封装好的数据list
            foreach (Dictionary<String, Object> dic in ret_list)
            {
                RecordDetail rd = new RecordDetail();

                rd.Med_name = dic["name"].ToString();
                rd.Med_price = dic["price"].ToString();
                //MessageBox.Show(dic["price"].ToString());
                rd.Med_info = dic["info"].ToString();
                rd.Id = (int)dic["id"];
                rd.User_id = (int)dic["use_id"];
                rd.Address = dic["address"].ToString();
                rd.Remark = dic["remark"].ToString();
                rd.Time = dic["time"].ToString();
                rdlist.Add(rd);//加入list做返会
            }
            return rdlist;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">删除的id</param>
        public void delete(int id)
        {
            rd.delete(id);
        }
    }
}
